<template>
  <div class="login">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
      <span class="login-left"></span>
       <img :src="imgUrl[4]" alt="" id="login-back">
      <span class="login-right" >
        <img :src="imgUrl[0]" alt="" class="ligin-img">
        <div class="login-mian">
          <div class="login-title">燃烧动力学数据质量评估系统</div>
          <div>
             <span class="mesTitle"> 账号</span>
             <div class="inps">
               <img :src="imgUrl[1]" alt="">
               <input type="text" v-model="loginForm.username"  placeholder="账号">
             </div>
             <div class="rules">xxxxxxxxxxx</div>
          </div>
          <div>
             <span class="mesTitle">密码</span>
             <div class="inps">
               <img :src="imgUrl[2]" alt="">
               <input type="password" v-model="loginForm.password"  placeholder="密码">
             </div>
              <div class="rules">xxxxxxxxxxx</div>
            <div class="ceCode" v-if="captchaOnOff">
              <img :src="imgUrl[3]" alt="">
              <input style="width: 150px" type="text" v-model="loginForm.code" placeholder="验证码">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
             <div class="rules" :style="{visibility:codeShow}">验证码错误</div>
          </div> 
          <div class="login-btn"  @click.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
               <span v-else>登 录 中...</span>
          </div>
        </div>
      </span>
       </el-form>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt';
export default {
  name: 'HelloWorld',
  props: {
  },
  data () {
    return {
        imgUrl:[require("@/assets/logo/Maskgroup.png"),require("@/assets/logo/Frame@3x.png"),require("@/assets/logo/Frame2@3x.png"),require("@/assets/logo/Frame3@3x.png"),require("@/assets/logo/Maskgroup@3x.png")],
        codeUrl: "",
        cookiePassword: "",
        codeShow:'hidden',
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe');
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.codeShow = 'hidden'
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.codeShow = 'visible'
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    },
    mounted () {

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
.login{
   height: 100vh;
   width: 100vw;
 }
.login-left{
  display: inline-block;
  width: 40vw;
  height: calc(100vh - 4px);
  background-color: #bd4541;
}
.login-right{
  display: flex !important;
  height: 80vh;
  width: 75vw;
  position: absolute;
  top:10vh;
  left: 20vw;
}
#login-back{
  height: 100vh;
  width: 100vw;
  position: absolute;
  left: 0vw;
  top: -8vh;
}
.ligin-img{
  width: 35vw;
  height: 80vh;
  margin-right: 8vw;
}
.login-mian{
   width: 40vw;
   padding: 7vw 0 0 0;
   box-sizing: border-box;
}
.login-title{
  font-size: 34px;
  font-weight: bold;
  margin-bottom: 6vh;
  letter-spacing: 2px;
}
.mesTitle{
  display: inline-block;
  color: #666;
  margin-bottom: 20px;
}
.inps{
  display: flex;
  width: 30vw;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10px;
}
.ceCode{
  display: flex;
  width: 30vw;
  align-items: center;
  background-color: #f8f8f8;
  padding: 15px 25px ;
  border-radius: 8px;
  box-sizing: border-box;
   input{
     margin-right: 20px;
     background-color: #f8f8f8;
   } 
}
 input{
 border: none;
 font-size: 20px;
 color: #333;
 padding: 5px 0 0px 5px;
}
 input:focus{
  outline: none;
}
.login-mian img{
  width: 24px;
  height: 24px;
  margin-right: 10px;
}
.login-btn{
  height: 6.5vh;
  width: 30vw;
  line-height: 6.5vh;
  text-align: center;
  border-radius: 30px;
  background-color: #bd4541;
  color: #fff;
  font-size: 22px;
  letter-spacing: 15px;
  margin-top: 80px;
  opacity: 1;
  box-sizing: border-box;
}
.login-code-img {
  width: 16vw !important;
  height: 8vh !important;
}
.rules{
  height: 25px;
  line-height: 25px;
  margin-bottom: 5px;
  color: red;
  visibility: hidden;
}
</style>
